package com.cqgcxy.shopping.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqgcxy.shopping.common.Pager;
import com.cqgcxy.shopping.domain.Goods;
import com.cqgcxy.shopping.service.GoodsService;



@WebServlet(urlPatterns="/user/list.do")
public class GoodsListServlet extends HttpServlet{

	private static final long serialVersionUID = -7271041558224551183L;
	private Logger logger =LoggerFactory.getLogger(getClass());
	private GoodsService goodsService=new GoodsService();
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		logger.debug("执行list");
		try {
			
			//接受参数
			String goodname = req.getParameter("goodname");
			String gooddesc=req.getParameter("gooddesc");
			
			String pageSize=req.getParameter("pageSize");
			String currentPage=req.getParameter("currentPage");
			pageSize=pageSize==null?"5":pageSize;
			currentPage=currentPage==null?"1":currentPage;
			
			Goods goods = new Goods();
			goods.setGoodname(goodname);
			goods.setGooddesc(gooddesc);
			Pager pager=new Pager();
			
			if(pageSize==null||pageSize.length()<1) {
				pager.setPageSize(5);
			}else {
				pager.setPageSize(Integer.valueOf(pageSize));
			}
			if(currentPage==null||currentPage.length()<1) {
				pager.setCurrentPage(1);
			}else {
				pager.setCurrentPage(Integer.valueOf(currentPage));
			}
			if(goodname==null) {
				goods.setGoodname("");
			}
			if(gooddesc==null) {
				goods.setGooddesc("");
			}
			//数据校验
			//类型转换
			//执行业务
			List<Goods> goodsl=goodsService.findPager(goods,pager);
			//跳转
			req.setAttribute("goodsl", goodsl);
			req.setAttribute("pager", pager);
			req.getRequestDispatcher("/productjsp").forward(req, resp);
			logger.debug("商品分页查询成功");
		}catch (Exception e) {
			//跳转
			logger.error(e.getMessage());
			req.setAttribute("error", e.getMessage());
			req.getRequestDispatcher("/product.jsp").forward(req, resp);
		}
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

}
